package com.imu.chatmodule.LongConnection.handler.nettyService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.imu.chatmodule.LongConnection.model.LongConnectionChatDTO;
import com.imu.chatmodule.LongConnection.model.ReturnMsgVo;
import com.imu.chatmodule.LongConnection.model.SendMsgVo;
import com.imu.chatmodule.LongConnection.model.msg.FileMsg;
import com.imu.chatmodule.LongConnection.model.msg.FileMsgVo;
import com.imu.chatmodule.LongConnection.model.msg.TextMsg;
import com.imu.chatmodule.LongConnection.model.msg.TextMsgVo;
import com.imu.chatmodule.LongConnection.util.LongConnectionMsgUtil;
import com.imu.chatmodule.ab.entity.Chatrecord;
import com.imu.chatmodule.ab.entity.FileData;
import com.imu.chatmodule.ab.service.IChatrecordService;
import com.imu.chatmodule.ab.service.IFileDataService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Component
public class NonChannelChatService {
    @Autowired
    private IChatrecordService chatrecordService;
    @Autowired
    private IFileDataService fileDataService;


    private static NonChannelChatService mclass;

    @PostConstruct
    public void init() {
        mclass = this;
    }

    private static HashMap<Integer, IChatHandle> map = new HashMap<>();
    private static final ObjectMapper mapper = new ObjectMapper();
    private static final Mapper dozerMapper = DozerBeanMapperBuilder.buildDefault();

    static {
        map.put(LongConnectionChatDTO.TEXT, ((ctx, chatDTO, senderId) -> {
            TextMsg textMsg = mapper.convertValue(chatDTO.getMessage(), TextMsg.class);
            Chatrecord chatrecord = Chatrecord.ChatRecordUnRead(senderId, chatDTO.getReceiverId(), textMsg.getText(), chatDTO.getMsgType());
            mclass.chatrecordService.save(chatrecord);
            TextMsgVo msgVo = dozerMapper.map(chatrecord, TextMsgVo.class);
            ctx.writeAndFlush(LongConnectionMsgUtil.ObjectToWSTextResult(new ReturnMsgVo(ReturnMsgVo.SEND_SUCCESS,
                    new SendMsgVo(chatDTO.getChatId(), msgVo))));

        }));
        map.put(LongConnectionChatDTO.FILE, ((ctx, chatDTO, senderId) -> {
            FileMsg fileMsg = mapper.convertValue(chatDTO.getMessage(), FileMsg.class);
            RestTemplate template = new RestTemplate();
            ResponseEntity<String> response = template.postForEntity("http://localhost:8080/fileUploadByStream", fileMsg, String.class);
            Chatrecord chatrecord = Chatrecord.ChatRecordUnRead(senderId, chatDTO.getReceiverId(), "", chatDTO.getMsgType());
            mclass.chatrecordService.save(chatrecord);
            FileData fileData = new FileData(chatrecord.getMsgId(), response.getBody(), fileMsg.getFileName());
            mclass.fileDataService.save(fileData);
            FileMsgVo fileMsgVo = dozerMapper.map(chatrecord, FileMsgVo.class);
            fileMsgVo.setFileName(fileData.getFileName()).setUrl(fileData.getUrl());
            ctx.writeAndFlush(LongConnectionMsgUtil.ObjectToWSTextResult(new ReturnMsgVo(ReturnMsgVo.SEND_SUCCESS, new SendMsgVo(chatDTO.getChatId(), fileMsgVo))));
        }));
    }

    public static void doChat(Integer msgType, ChannelHandlerContext ctx, LongConnectionChatDTO chatDTO, String senderId) throws JsonProcessingException {
        map.get(msgType).handle(ctx, chatDTO, senderId);
    }

}
